import request from "@/services/interceptors";
import type { ActionType, ProColumns } from "@ant-design/pro-components";
import { ProTable } from "@ant-design/pro-components";
import { useRef } from "react";
import { enumToObject, QuestionTypeEnum } from "@/utils/enums";
import { Button, message, Modal } from "antd";
import { ExclamationCircleFilled } from "@ant-design/icons";
import { RecommonObjectiveType } from "@/types";
import MyDrawer from "@/components/MyDrawer";
import ObjectDetail from "@/pages/topic/ObjectiveTopic/ObjectDetail";
const ObjectiveTopic = () => {
  const actionRef = useRef<ActionType>(null);
  const columns: ProColumns<RecommonObjectiveType>[] = [
    {
      title: "学员ID",
      dataIndex: "studentId",
      hideInSearch: true,
      hideInTable: true,
    },
    {
      title: "学员姓名",
      dataIndex: "studentName",
    },
    {
      title: "题目编号",
      dataIndex: "questionCode",
      hideInSearch: true,
      render: (text, record) => {
        return (
          <MyDrawer
            title="客观题详情"
            drawerContent={<ObjectDetail questionId={record?.questionId} />}
          >
            <a type="link">{text}</a>
          </MyDrawer>
        );
      },
    },
    {
      title: "题干",
      dataIndex: "questionStem",
      ellipsis: true,
    },

    {
      title: "题目类型",
      dataIndex: "questionType",
      valueEnum: enumToObject(QuestionTypeEnum),
    },
    {
      title: "课程",
      dataIndex: "courseId",
      hideInTable: true,
    },
    {
      title: "课程名称",
      dataIndex: "courseName",
      hideInSearch: true,
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      hideInSearch: true,
    },
    {
      title: "更新时间",
      dataIndex: "updateTime",
      hideInSearch: true,
    },
    {
      title: "状态",
      dataIndex: "questionStatus",
      initialValue: "1",
      valueEnum: {
        0: {
          text: "已删除",
          status: "error",
        },
        1: {
          text: "正常",
          status: "success",
        },
      },
    },
    {
      title: "操作",
      valueType: "option",
      key: "option",
      render: (text, record, _, action) => [
        <a
          onClick={() => {
            Modal.confirm({
              title: "提示",
              icon: <ExclamationCircleFilled />,
              content: "确认删除吗？",
              okText: "确认",
              cancelText: "取消",
              onOk: async () => {
                await request.sgks.recommendQuestionDeleteDelete({
                  studentId: record.studentId,
                  questionId: record.questionId,
                });
                message.success("删除成功");
                action?.reload();
              },
            });
          }}
        >
          删除
        </a>,
      ],
    },
  ];

  return (
    <ProTable<RecommonObjectiveType>
      columns={columns}
      actionRef={actionRef}
      cardBordered
      request={async (params, sort, filter) => {
        console.log(params, sort, filter);
        const res = await request.sgks.recommendQuestionListCreate({
          ...params,
          pageNo: params.current,
          pageSize: params.pageSize,
        });
        return {
          total: res.data.totalCount,
          data: res.data.records,
        };
      }}
      columnsState={{
        persistenceKey: "ObjectiveTopic_List",
        persistenceType: "localStorage",
        defaultValue: {
          option: { fixed: "right", disable: true },
        },
      }}
      rowKey="id"
      search={{
        labelWidth: "auto",
      }}
      options={{
        setting: {
          listsHeight: 400,
        },
      }}
      pagination={{
        pageSize: 10,
      }}
      dateFormatter="string"
    />
  );
};

export default ObjectiveTopic;
